package com.cgbl.service.ui

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.service.BR
import com.cgbl.service.R
import com.cgbl.service.adapter.TeamManagerAdapter
import com.cgbl.service.databinding.AppActivityTeamManagerBinding
import com.cgbl.service.vm.TeamManagerViewModel
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.TeamListBean
import com.robot.base.util.LiveDataEventManager
import com.robot.base.util.arouter.ARouterUtil

@Route(path = AppConstants.Router.Project.A_TEAM_MANAGER)
class TeamListActivity : GLBaseActivity<AppActivityTeamManagerBinding, TeamManagerViewModel>() {

    lateinit var teamManagerAdapter: TeamManagerAdapter

    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_team_manager

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initTiterBar()
        teamManagerAdapter = TeamManagerAdapter()
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = teamManagerAdapter
        }
        teamManagerAdapter.setOnItemClickListener { _, _, position ->
            ARouterUtil.goToActivityWithBundle(AppConstants.Router.Project.A_ADD_TEAM,
                Bundle().apply {
                    putString(AppConstants.BundleKey.ID, teamManagerAdapter.data.get(position).id)
                })
        }
        binding.ivAdd.setOnClickListener {
            ARouterUtil.goToActivity(AppConstants.Router.Project.A_ADD_TEAM)
        }
        viewModel.fetchHomeTeam()
    }

    override fun initViewObservable() {
        viewModel.uc.homeTeamDataEvent.observe(this) {
            teamManagerAdapter.setNewInstance(it as MutableList<TeamListBean>?)
        }
        LiveDataEventManager.observerRefreshTeamEvent(this){
            viewModel.fetchHomeTeam()
        }
    }

    private fun initTiterBar() {
        val titleView = findViewById<TextView>(R.id.tv_title)
        val backView = findViewById<ImageView>(R.id.iv_back)
        titleView?.text = "团队管理"
        backView?.setOnClickListener { finish() }
    }
}